/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.reciever.process;

import dom.Command;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author julian.pena
 */
public class Encrypt extends ProcessReciever{

    public Encrypt(InputStream inputStream, OutputStream outputStream) {
        super(inputStream, outputStream);
    }

    public Encrypt(InputStream inputStream, OutputStream outputStream, Command command) {
        super(inputStream, outputStream, command);
    }

    @Override
    public void action() throws Exception {
        while (fileName.equals("")) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException interruptedException) {}
        }
        stop = false;
        String fileNameEncrypt;
        byte[] buf = new byte[BUFFER_SIZE];
        while (true) {
            fileNameEncrypt = new String(fileName + ".crypt");
            System.out.println("Encriptando: " + fileName);
            this.notifyStop(fileNameEncrypt);
            int len;
            while ((len = inputStream.available()) > -1) {
                if (len<= 0) {
                    if (stop) {
//                        System.out.println("disponible: " + inputStream.available());
                        break;
                    }
                }
                len = inputStream.read(buf);
                outputStream.write(buf, 0, len);
            }
            stop = false;
            if (fileName == null) {
                System.out.println("Termino de encriptar");
                super.notifyStop(null);
                outputStream.close();
                inputStream.close();
                return;
            }
        }
    }

    @Override
    public boolean isInputReady() {
        if (inputStream != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isOutputReady() {
        if (outputStream != null) {
            return true;
        } else {
            return false;
        }
    }
    
}
